import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class Canvas here.
 * World where always going to be working on called canvas.
 * 
 * @author Christian Pérez Zavalija
 * @version 25-Jun-2013
 */
public class Canvas extends World
{
    private BtnNew btnnew;
    private BtnNew btncredits;
    private BtnNew btnhelp;
    private Pin pin1;
    private Pin pin2;
    
    /**
     * Constructor for objects of class Canvas. creates a world of 800 * 800 with cell size of 1 pixel,
     * while the objects in the world to get out of it.
     * Pin Initializes two variables to null and loads the menu buttons, the power bar, and the
     * bar circuit.
     */
    public Canvas()
    {
        super(800, 800, 1,true);
        pin1=null;
        pin2=null;

        prepare();
        //System.out.println(numberOfObjects ());
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        CircuitsBar circuitsbar = new CircuitsBar();
        addObject(circuitsbar, 75, 400);
        PowerBar powerbar = new PowerBar();
        addObject(powerbar, 738, 400);

        BtnCredits btncredits = new BtnCredits();
        addObject(btncredits, 400, 31);
        BtnHelp btnhelp = new BtnHelp();
        addObject(btnhelp, 470, 31);
        BtnNew btnnew = new BtnNew();
        addObject(btnnew, 330, 31);
        
        Dump dump = new Dump();
        addObject(dump, 593, 658);
        dump.setLocation(745, 758);
        dump.setLocation(741, 748);
        dump.setLocation(744, 754);
        dump.setLocation(743, 755);
    }
    
    /**
     * This method saves the first and second selected pin, load and modifies selected second pin.
     * If you clicked on the second pin call method createLine().
     * 
     * @param Pin aPin
     */
    public void pinSelecc(Pin aPin)
    {
        if(pin1==null)
            pin1=aPin;
        else{
            pin2=aPin;
            pin2.setCharge(pin1.charge);    //El segundo pin se carga con lo que tiene el primero
        }
        if(pin2!=null)
            this.createLine();
    }
    
    /**
     * This method creates a line of blue or red depending on the load of the first selected pin or black
     * if your load is garbage. After the creation of the line, pin variables return null, for reselect
     * to other pins.
     */
     public void createLine() 
    {
        GreenfootImage imgLine=getBackground();
        if(pin1.charge==0)
            imgLine.setColor(java.awt.Color.blue);
        if(pin1.charge==1)
            imgLine.setColor(java.awt.Color.red);
        if(pin1.charge==2)
            imgLine.setColor(java.awt.Color.black);
        imgLine.drawLine(pin1.getX(),pin1.getY(),pin2.getX(),pin2.getY());
        setBackground(imgLine);
        pin1=null;
        pin2=null;
    }
    
    /**
     * Allows interaction with the buttons where, using the mouse or the arrow keys: 'n' = New,
     * 'c' = Credits, 'h' = Help.
     */
    public void act()
    {
        MouseInfo mouse=Greenfoot.getMouseInfo();
        if(Greenfoot.mouseClicked(BtnNew.class)||Greenfoot.isKeyDown("n")){
            List list=getObjects(null);
            removeObjects(list);
            //prepare();
            Greenfoot.setWorld(new Canvas());
        }
        if(Greenfoot.mousePressed(BtnHelp.class)||Greenfoot.isKeyDown("h")){
            Help help = new Help();
            Greenfoot.setWorld(help);
        }
        if(Greenfoot.mousePressed(BtnCredits.class)||Greenfoot.isKeyDown("c")){
            Credits credits = new Credits();
            Greenfoot.setWorld(credits);
        }
    }
}
